﻿using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Windows.Controls;
using Microsoft.Practices.Prism.Commands;
using ValidationResult = System.ComponentModel.DataAnnotations.ValidationResult;

namespace ManagerClient.Messaging.Logic
{
    public class SendMailViewModel : ViewModelBase
    {
        private string message;
        private string recipientMail;
        private string senderMail;
        private string senderName;
        private string subject;

        public SendMailViewModel()
        {
            GetMailCommand = new DelegateCommand<object>(SendMail);
        }

        public DelegateCommand<object> GetMailCommand { get; private set; }

        [DisplayName("SenderName")]
        [Required]
        public string SenderName
        {
            get { return senderName; }
            set
            {
                senderName = value;
                RaisePropertyChanged(() => SenderName);
            }
        }

        [DisplayName("SenderMail")]
        [Required]
        public string SenderMail
        {
            get { return senderMail; }
            set
            {
                senderMail = value;
                RaisePropertyChanged(() => SenderMail);
            }
        }

        [DisplayName("RecipientMail")]
        [Required]
        public string RecipientMail
        {
            get { return recipientMail; }
            set
            {
                recipientMail = value;
                RaisePropertyChanged(() => RecipientMail);
            }
        }

        [Required]
        public string Subject
        {
            get { return subject; }
            set
            {
                subject = value;
                RaisePropertyChanged(() => Subject);
            }
        }

        [Required]
        public string Message
        {
            get { return message; }
            set
            {
                message = value;
                RaisePropertyChanged(() => Message);
            }
        }


        public string this[string property]
        {
            get
            {
                PropertyInfo propertyInfo = GetType().GetProperty(property);
                var results = new List<ValidationResult>();

                bool result = Validator.TryValidateProperty(
                    propertyInfo.GetValue(this, null),
                    new ValidationContext(this, null, null)
                        {
                            MemberName = property
                        }, results
                    );

                if (!result)
                {
                    ValidationResult validationResult = results.First();
                    return validationResult.ErrorMessage;
                }
                return string.Empty;
            }
        }

        private void SendMail(object passwordBox)
        {
            var p = (PasswordBox) passwordBox;
            var mp = new MailProvider();
            mp.SendMessage(new MailInfo
                {
                    Body = Message,
                    Credentials =
                        new UserCredentials
                            {
                                ClientlientType = SmtpClientType.Google,
                                Login = SenderMail,
                                Password = p.Password
                            },
                    Subject = Subject,
                    To = RecipientMail
                });
        }
    }
}